package cdu.sl.aichatdemo.pojo.entity.token;

import cdu.sl.aichatdemo.pojo.entity.User;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @Description
 * @Author sunlin5@asiainfo.com
 * @Date 2025/8/21
 * @Version 1.0
 */
public class EmailCodeAuthenticationToken extends AbstractAuthenticationToken {


    /**
     * 存储邮箱(认证之前)和认证信息(认证完成后)
     */
    private final Object principal;

    /**
     * 存储验证码
     */
    private final Object credentials;


    /**
     * 没有认证时的构造方法
     */
    public EmailCodeAuthenticationToken(String email, String code) {
        super(null);
        this.principal = email;
        this.credentials = code;
        setAuthenticated(false);
    }

    /**
     * 认证通过后的构造方法
     */
    public EmailCodeAuthenticationToken(User userDetails, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = userDetails;
        this.credentials = null;    // 验证通过之后 可以不再需要验证码
        setAuthenticated(true);
    }


    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }


}
